package com.unicom.office.impl;

import com.unicom.office.builder.DocumentBuilder;
import com.unicom.office.unit.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class DocxBuilder extends DocumentBuilder {
    protected Object buildDocument(DocumentUnit unit) {
        //XWPFDocument document = new XWPFDocument();
        XWPFDocument srcDocument = (XWPFDocument) unit.getSource();
        SectionUnit sectionUnit = (SectionUnit) unit.getChildren().get(0);

//        document.setEvenAndOddHeadings(srcDocument.getEvenAndOddHeadings());
//        document.setMirrorMargins(srcDocument.getMirrorMargins());
//        document.setTrackRevisions(srcDocument.isTrackRevisions());
//        document.setZoomPercent(srcDocument.getZoomPercent());
//        document.setCommitted(srcDocument.isCommitted());

        for (OfficeUnit child : sectionUnit.getChildren()) {
            build2(child, srcDocument);
        }

        return srcDocument;
    }

    protected Object buildSection(SectionUnit unit, Object parent) {
        return null;
    }

    protected Object buildDocTable(DocTableUnit unit, Object parent) {
        return null;
    }

    protected Object buildRow(RowUnit unit, Object parent) {
        return null;
    }

    protected Object buildCell(CellUnit unit, Object parent) {
        return null;
    }

    protected Object buildParagraph(ParagraphUnit unit, Object parent) {
        //XWPFDocument document = (XWPFDocument) parent;
        //XWPFParagraph paragraph = document.createParagraph();
        XWPFParagraph srcParagraph = (XWPFParagraph)unit.getSource();

        if(unit.isJustResource()){
            return srcParagraph;
        }else {
            int size = srcParagraph.getRuns().size();

            // 先添加再删除
            for (OfficeUnit child : unit.getChildren()) {
                build2(child, srcParagraph);
            }

            for (int i = size; i > 0; i--) {
                srcParagraph.removeRun(i - 1);
            }

            return srcParagraph;
        }

        // 设置样式
//        paragraph.setAlignment(srcParagraph.getAlignment());
//        paragraph.setBorderBetween(srcParagraph.getBorderBetween());
//        paragraph.setBorderBottom(srcParagraph.getBorderBottom());
//        paragraph.setBorderLeft(srcParagraph.getBorderLeft());
//        paragraph.setBorderRight(srcParagraph.getBorderRight());
//        paragraph.setBorderTop(srcParagraph.getBorderTop());
//        paragraph.setFirstLineIndent(srcParagraph.getFirstLineIndent());
//        paragraph.setFontAlignment(srcParagraph.getFontAlignment());
//        paragraph.setIndentationFirstLine(srcParagraph.getIndentationFirstLine());
//        paragraph.setIndentationHanging(srcParagraph.getIndentationHanging());
//        paragraph.setIndentationLeft(srcParagraph.getIndentationLeft());
//        paragraph.setIndentationRight(srcParagraph.getIndentationRight());
//        paragraph.setIndentFromLeft(srcParagraph.getIndentFromLeft());
//        paragraph.setIndentFromRight(srcParagraph.getIndentationRight());
//        paragraph.setKeepNext(srcParagraph.isKeepNext());
//        paragraph.setNumID(srcParagraph.getNumID());
//        paragraph.setNumILvl(srcParagraph.getNumIlvl());
//        paragraph.setPageBreak(srcParagraph.isPageBreak());
//        paragraph.setSpacingAfter(srcParagraph.getSpacingAfter());
//        paragraph.setSpacingAfterLines(srcParagraph.getSpacingAfterLines());
//        paragraph.setSpacingBefore(srcParagraph.getSpacingBefore());
//        paragraph.setSpacingBeforeLines(srcParagraph.getSpacingBeforeLines());
//        paragraph.setSpacingBetween(srcParagraph.getSpacingBetween());
//        paragraph.setSpacingLineRule(srcParagraph.getSpacingLineRule());
//        paragraph.setStyle(srcParagraph.getStyle());
//        paragraph.setVerticalAlignment(srcParagraph.getVerticalAlignment());
        //paragraph.setWordWrapped(srcParagraph.isWordWrapped());

    }

    protected Object buildTextbox(TextboxUnit unit, Object parent) {
        return null;
    }

    protected Object buildTextrange(TextRangeUnit unit, Object parent) {
        XWPFParagraph paragraph = (XWPFParagraph) parent;
        XWPFRun run = paragraph.createRun();
        XWPFRun srcRun = (XWPFRun) unit.getSource();
        run.setText(unit.getText());

        // 设置样式
        run.setBold(srcRun.isBold());
        run.setColor(srcRun.getColor());
        run.setCapitalized(srcRun.isCapitalized());
        run.setCharacterSpacing(srcRun.getCharacterSpacing());
        run.setDoubleStrikethrough(srcRun.isDoubleStrikeThrough());
        run.setEmbossed(srcRun.isEmbossed());
        run.setFontFamily(srcRun.getFontFamily());
        run.setFontSize(srcRun.getFontSize());
        run.setImprinted(srcRun.isImprinted());
        run.setItalic(srcRun.isItalic());
        run.setKerning(srcRun.getKerning());
        run.setLang(srcRun.getLang());
        run.setShadow(srcRun.isShadowed());
        run.setSmallCaps(srcRun.isSmallCaps());
        run.setStrikeThrough(srcRun.isStrikeThrough());
        run.setStyle(srcRun.getStyle());
        run.setSubscript(srcRun.getSubscript());
        run.setTextPosition(srcRun.getTextPosition());
        run.setTextScale(srcRun.getTextScale());
        run.setUnderline(srcRun.getUnderline());
        run.setUnderlineThemeColor(srcRun.getUnderlineColor());
        run.setVanish(srcRun.isVanish());

        return run;
    }

    protected Object buildPicture(PictureUnit unit, Object parent) {
        return null;
    }

    public void writeFile(Object result, String targetPath) {
        if(!(result instanceof XWPFDocument)) {
            throw new RuntimeException("不能输出文件");
        }

        XWPFDocument wb = (XWPFDocument)result;
        try {
            FileOutputStream file = new FileOutputStream(targetPath);
            wb.write(file);
            file.close();
        }catch(Exception ex){
            throw new RuntimeException(ex.getMessage());
        }
    }
}
